package cn.mrlong.base.utils

import android.app.ActivityManager
import android.content.Context
import android.content.pm.PackageManager
import android.os.Process

/**
 * 作者：BINGO
 * 时间：2021/03/29 11:01
 */
class AppUtils private constructor() {
    companion object {
        /**
         * 判断指定的程序包名是否安装
         * @param context Context
         * @param packageName String 指定的程序的包名
         * @return Boolean
         */
        fun isAvilible(context: Context, packageName: String): Boolean {
            val packageManager = context.packageManager;
            //获取已经安装的程序包名
            val installedPackages = packageManager.getInstalledPackages(0)
            //存储已经安装程序的包名
            var packageNames = ArrayList<String>()
            installedPackages.forEach {
                val packName = it.packageName;
                //获得具体的包名，存入list中
                packageNames.add(packName)
            }
            //判断packageName中是否需要查询的程序包名
            return packageNames.contains(packageName)
        }

        /**
         * 判断当前app的包名
         * @param context Context
         * @return String?
         */
        fun getAppPackageName(context: Context): String? {
            //获取pid
            val pid = android.os.Process.myPid()
            //任务管理器
            val manager: ActivityManager =
                context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
            //获取正在运行的app信息
            val infos = manager.runningAppProcesses
            infos.forEach {
                if (it.pid == pid) {
                    return it.processName
                }
            }
            return null
        }

        /**
         * 获取应用程序名称
         */
        fun getAppName(context: Context): String? {
            try {
                val packageManager = context.packageManager
                val packageInfo = packageManager.getPackageInfo(
                    context.packageName, 0
                )
                val labelRes = packageInfo.applicationInfo.labelRes
                return context.resources.getString(labelRes)
            } catch (e: PackageManager.NameNotFoundException) {
                e.printStackTrace()
            }
            return null
        }

        /**
         * [获取应用程序版本]
         *
         * @param context
         * @return 当前应用的版本
         */
        fun getVersionCode(context: Context): Long {
            var code = -1L
            try {
                val packageManager = context.packageManager
                val packageInfo = packageManager.getPackageInfo(context.packageName, 0)
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) {
                    code = packageInfo.longVersionCode
                } else {
                    code = packageInfo.versionCode.toLong()
                }
            } catch (e: Exception) {
                code = -1L
                e.printStackTrace()
            } finally {
                return code
            }
        }

        /**
         * [获取应用程序版本名称信息]
         *
         * @param context
         * @return 当前应用的版本名称
         */
        fun getVersionName(context: Context): String? {
            try {
                val packageManager = context.packageManager
                val packageInfo = packageManager.getPackageInfo(
                    context.packageName, 0
                )
                return packageInfo.versionName
            } catch (e: PackageManager.NameNotFoundException) {
                e.printStackTrace()
            }
            return null
        }

        /**
         * 重启整个APP
         * @param context
         * @param Delayed 延迟多少毫秒
         */
        fun restartAPP(context: Context?, Delayed: Long) {
            /**开启一个新的服务，用来重启本APP */ /*
        Intent intent1=new Intent(context, killSelfService.class);
        intent1.putExtra("PackageName",context.getPackageName());
        intent1.putExtra("Delayed",Delayed);
        context.startService(intent1);*/
            /**杀死整个进程 */
            Process.killProcess(Process.myPid())
        }

        /***重启整个APP */
        fun restartAPP(context: Context?) {
            restartAPP(context, 2000)
        }
    }

}